package engine;

import gui.TextGUI;

import java.awt.*;
import java.io.*;
import java.util.Properties;
import java.util.logging.LogManager;

/**
 * This class create the world and make time flow
 */
public class Starter {
    public static void main(String[] args) {
        /*try {
            System.out.println("Enter size of the world (x, y)");
            BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
            String str = buff.readLine();
            String[] strArr = str.split(" ");
            int[] size = new int[strArr.length];
            for (int i = 0; i < 2; i++)  {
                size[i] = Integer.parseInt(strArr[i]);
            }
            World world1 = new World(size[0], size[1]);*/
        try {
            LogManager.getLogManager().readConfiguration(
                    Starter.class.getResourceAsStream("/logging.properties"));
        } catch (IOException e) {
            System.err.println("Could not setup logger configuration: " + e.toString());
        }
        try {
            Properties prop = new Properties();
            //prop.load(Starter.class.getResourceAsStream("/start_config.txt"));
            InputStream in = Starter.class.getResourceAsStream(fileNameBuilder("/start_config.txt"));
            prop.load(in);
            in.close();
            int length = Integer.valueOf(prop.getProperty("field_length", "1"));
            int width = Integer.valueOf(prop.getProperty("field_width", "1"));
            String[] fileList = prop.getProperty("creature_files").split(" ");
            if (length <= 0 || width <= 0) {
                throw new IOException();
            }
            World world1 = new World(length, width);
            TextGUI gui1 = new TextGUI();
            gui1.startGUI(world1);
            for (String str : fileList) {
                try {
                    createAnimalsFromFile(fileNameBuilder(str), world1);
                } catch (IOException ex) {
                    System.out.println("File " + str + " is broken.");
                }
            }
            gui1.drawWorldPanel();
            gui1.drawCellPanel();

        } catch (FileNotFoundException ex) {
            System.out.println("Start.config file is not found.");
        } catch (IOException ex) {
            ex.printStackTrace();
            //System.out.println(StackT);
            World world1 = new World(1, 1);
            TextGUI gui1 = new TextGUI();
            gui1.startGUI(world1);
        }

    }


    private static void createAnimalsFromFile(String source, World world) throws IOException {
        Properties p = new Properties();
        InputStream in = Starter.class.getResourceAsStream(source);
        p.load(in);
        in.close();
        int numberOfCreatures = Integer.parseInt(p.getProperty("Number_of_Creatures", "1"));
        String[] strArr;
        int[] intArr = new int[2];

        for (int i = 0; i < numberOfCreatures; i++) {
            intArr = val(p.getProperty("Cell").split(" "));
            Cell cell = world.field[intArr[0] * world.fieldWidth + intArr[1]];
            SexType sex = ((Math.random() * 100 - Integer.valueOf(p.getProperty("Male_percents"))) < 0) ? SexType.male : SexType.female;
            intArr = val(p.getProperty("Width").split(" "));
            int genWidth = intArr[0] + Creature.random(intArr[1]);
            intArr = val(p.getProperty("Speed").split(" "));
            int genSpeed = intArr[0] + Creature.random(intArr[1]);
            intArr = val(p.getProperty("Food_need").split(" "));
            int foodNeed = intArr[0] + Creature.random(intArr[1]);
            intArr = val(p.getProperty("Stamina").split(" "));
            int stamina = intArr[0] + Creature.random(intArr[1]);
            intArr = val(p.getProperty("Color_RGB").split(" "));
            Color color = Creature.colorConstr(intArr[0] + Creature.random(intArr[3]),
                    intArr[1] + Creature.random(intArr[3]),
                    intArr[2] + Creature.random(intArr[3]));
            intArr = val(p.getProperty("Meat_prefer", "50 10").split(" "));
            int meatPrefer = intArr[0] + Creature.random(intArr[1]);
            intArr = val(p.getProperty("Max_energy").split(" "));
            int maxEnergy = intArr[0] + Creature.random(intArr[1]);
            intArr = val(p.getProperty("Energy_recovery").split(" "));
            int energyRecovery = intArr[0] + Creature.random(intArr[1]);
            strArr = p.getProperty("Vision", "1 0.1").split(" ");
            float vision = Float.valueOf(strArr[0]) + Creature.random(Float.valueOf(strArr[1]));

            intArr = val(p.getProperty("Attack").split(" "));
            int attack = intArr[0] + Creature.random(intArr[1]);
            intArr = val(p.getProperty("Armor").split(" "));
            int armor = intArr[0] + Creature.random(intArr[1]);
            Creature creature = new Creature(cell, sex, genSpeed, foodNeed, stamina, genWidth, meatPrefer, color, maxEnergy, energyRecovery, vision, attack, armor);
            cell.myWorld.creaturesOfWorld.add(creature);
            cell.creaturesOfCell.add(creature);
        }
        System.out.println("Creature from " + source + " was successfully loaded.");
    }

    private static String fileNameBuilder(String name) {
        String fileName = Starter.class.getResource("/logging.properties").toString();
        if (fileName.startsWith("jar:")) {
            fileName = name;
        }
        if (fileName.contains("file:")) {
            fileName = "/properties" + name;
        }
        return fileName;
    }

    private static int[] val (String[] strArr) {
        int[] result = new int[strArr.length];
        for(int i = 0; i < strArr.length; i++) {
            result[i] = Integer.valueOf(strArr[i]);
        }
        return result;
    }
}


